package org.opentcs.kcvehicle.communication.kc.udp.agv.param.function.write;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import java.util.List;
import org.opentcs.kcvehicle.communication.kc.common.byteutils.ByteUtils;

public class WriteParam {
  //变量数量 1个字节 ,最大只支持 15 个变量
  private byte valueNum;
  //保留 3个字节
  private byte[] retain;
  //这里的length 就是 valueNum
  private List<WriteStrValue> values;

  public WriteParam(Integer valueNum, List<WriteStrValue> values) {
    this.valueNum = ByteUtils.usintTo1Byte(valueNum);
    this.retain = new byte[]{(byte) 0x00, (byte) 0x00, (byte) 0x00};
    this.values = values;
  }

  public byte[] toBytes() {
    ByteBuf byteBuf = Unpooled.buffer();
    byteBuf.writeByte(valueNum);
    byteBuf.writeBytes(retain);
    for (WriteStrValue value : values) {
      byteBuf.writeBytes(value.toBytes());
    }
    int i = byteBuf.writerIndex();
    byte[] bytes = new byte[i];
    byteBuf.readBytes(bytes);
    return bytes;
  }

}
